<?php //strip
/**
 * Widget with list of galleries (primarily targets for sidebars).
 * 
 * @author Ondrej Donek <ondrejd@gmail.com>
 * @category odPhotogalleryWpPlugin
 * @since 0.5
 */
class odPhotogalleryWpPlugin_ListWidget extends WP_Widget
{
  
  function odPhotogalleryWpPlugin_ListWidget() 
  {
		// Widget options
		$widget_ops = array('classname' 	=> 'odPhotogalleryWpPlugin_ListWidget', 
	                    'description' 	=> __('Widget with list of the newest photogalleries.', 
				                                    odPhotogalleryWpPlugin::$textdomain));
		// Widget control options
		$control_ops = array('width' 		 => 200, 
		                     'height' 		=> 350, 
							           'id_base' 		=> 'photogallerylist1-widget');
		// Create the widget
		$this->WP_Widget('photogallerylist1-widget', 
		                 __('Latest photogalleries', odPhotogalleryWpPlugin::$textdomain), 
										 $widget_ops, 
										 $control_ops);
  }
  
  function widget($args, $instance) 
  {
		extract($args);
	
		// User-selected settings
		$title = apply_filters('widget_title', $instance['title']);
		$maxcount = (int) $instance['limit'];
		$morelink = (isset($instance['morelink'])) ? $instance['morelink'] : false;
	
		// Before widget (defined by theme)
		echo $before_widget;
	
		// Title of widget (before and after defined by theme)
		if($title) {
		  echo $before_title . $title . $after_title;
		}
		
		// Print list of photogalleries
		global $od_photogallery_wpplugin;
		
		foreach(($galleries = odPhotogalleryWpPluginModel::get_galleries()) as $gallery) {
			// TODO Add option if user want to display photogalleries with no images
			if($gallery['images_count'] != '0') {
?>
		<p><a value="<?php echo $od_photogallery_wpplugin->get_public_url_for_gallery($gallery['ID']);?>"><?php echo mysql2date('j F Y', $gallery['created'], true);?>, <?php echo $gallery['title'];?></a></p>
<?php
			}
		}
		
		if($morelink === true) {
			echo '<h4><a href="' . $od_photogallery_wpplugin->get_public_url_for_gallery() . '">' . __('More', odPhotogalleryWpPlugin::$textdomain) . '</a></h4>';
		}
		
		// After widget (defined by theme)
		echo $after_widget;
  }
  
  function update($new_instance, $old_instance) 
  {
		$instance = $old_instance;
	
		// Strip tags (if needed) and update the widget settings
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['limit'] = intval($new_instance['limit']); // XXX Check if is given really number!!!
		$instance['morelink'] = $new_instance['morelink'];
	
		return $instance;
  }
  
  function form($instance) 
  {
		global $od_photogallery_wpplugin;
		$options = $od_photogallery_wpplugin->get_options();
		
		// Set up some default widget settings.
		$defaults = array('title' 		=> __('Latest photogalleries', odPhotogalleryWpPlugin::$textdomain), 
						  'limit' 		=> 7, 
						  'morelink' 	=> true);
		$instance = wp_parse_args((array) $instance, $defaults); 
?>
	<p>
	  <label for="<?php echo $this->get_field_id('title');?>"><?php echo __('Widget title', odPhotogalleryWpPlugin::$textdomain);?>:</label>
	  <input id="<?php echo $this->get_field_id('title');?>" name="<?php echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;"/>
	</p>
	<p>
	  <label for="<?php echo $this->get_field_id('limit');?>"><?php echo __('Max. items count', odPhotogalleryWpPlugin::$textdomain);?>:</label>
	  <input id="<?php echo $this->get_field_id('limit');?>" name="<?php echo $this->get_field_name('limit');?>" value="<?php echo $instance['limit'];?>"/>
	</p>
	<p>
	  <input class="checkbox" type="checkbox" <?php checked($instance['morelink'], true);?> id="<?php echo $this->get_field_id('morelink');?>" name="<?php echo $this->get_field_name('morelink');?>"/>
	  <label for="<?php echo $this->get_field_id('morelink');?>"><?php echo __('Display <em>More...</em> link?', odPhotogalleryWpPlugin::$textdomain);?></label>
	</p>
<?php
  }
  
}
